# target
TARGET=FreeRTOS

BUILD_DIR=build

ROOT_DIR=../..

# wch risc gcc12
CROSS_COMPILE=riscv-wch-elf-
CC=$(CROSS_COMPILE)gcc
AS=$(CROSS_COMPILE)gcc -x assembler-with-cpp
CP=$(CROSS_COMPILE)objcopy
SZ=$(CROSS_COMPILE)size
LD=$(CROSS_COMPILE)ld
DP=$(CROSS_COMPILE)objdump

# SRC 
USER_SRC_C      = $(wildcard user/*.c )
DRIVER_SRC_C    = $(wildcard $(ROOT_DIR)/Peripheral/*.c )

#freertos
FREERTOS_SRC_C  = $(wildcard $(ROOT_DIR)/FreeRTOS-Kernel/*.c )
FREERTOS_SRC_C += $(wildcard $(ROOT_DIR)/FreeRTOS-Kernel/portable/RISC-V/*.c )
FREERTOS_SRC_S += $(wildcard $(ROOT_DIR)/FreeRTOS-Kernel/portable/RISC-V/*.S )

# # .c替换为 .o 
# temp_objs_freertos  = $(patsubst %.c,%.o,$(FREERTOS_SRC_C)) 
 # # 去掉 ../../
# OBJS_FREERTOS =$(subst ../../,,$(temp_objs) )


INC  = -Iuser/inc 
INC += -I$(ROOT_DIR)/Peripheral/inc

INC += -I$(ROOT_DIR)/FreeRTOS-Kernel/include -I$(ROOT_DIR)/FreeRTOS-Kernel/portable/RISC-V -I$(ROOT_DIR)/FreeRTOS-Kernel/portable/RISC-V/chip_specific_extensions/RV32I_PFIC_no_extensions


SRC_LD=Link.ld
 

SRC_C += $(USER_SRC_C) $(DRIVER_SRC_C) $(FREERTOS_SRC_C) 
SRC_S += $(FREERTOS_SRC_S)

OBJC = $(addprefix $(BUILD_DIR)/,$(SRC_C:.c=.o))
OBJS = $(addprefix $(BUILD_DIR)/,$(SRC_S:.S=.o))
OBJ  = $(OBJC) $(OBJS)
# 去掉../
LINK_OBJ=$(subst ../,,$(OBJ))

#CFLAGS
CFLAGS= \
-march=rv32imacxw \
-mabi=ilp32 \
-msmall-data-limit=8 -msave-restore -fmax-errors=20 -Os -fmessage-length=0 -fsigned-char \
-ffunction-sections -fdata-sections -Wunused -Wuninitialized \
-g3 -O0 -g

CFLAGS+= \
--specs=nano.specs \
--specs=nosys.specs    

LIBS=\
-lm
#如果程序并不实际使用系统函数，但是某些代码引用了系统函数，那么可以引入libnosys，以便通过编译。
#LIBS+=-lnosys

# LD FLAGS
LDFLAGS= $(CFLAGS) \
-nostartfiles -Xlinker --gc-sections -Xlinker --print-memory-usage -Wl,-Map,"$(TARGET).map" 

LDFLAGS+=-T$(SRC_LD) $(LIBS)

LDFLAGS += -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref
 

#SET PATH %.c %.S
vpath %.c $(sort $(dir $(SRC_C)))
vpath %.S $(sort $(dir $(SRC_S)))
 

ELF=$(BUILD_DIR)/$(TARGET).elf
BIN=$(BUILD_DIR)/$(TARGET).bin
DIS=$(BUILD_DIR)/$(TARGET).dis 

.PHONY : all clean d c download test  AS  rebuild

all: $(BUILD_DIR) $(OBJ) $(ELF) $(BIN) $(DIS)  
 

$(BUILD_DIR):
	@mkdir -p $@

# cc .c
 $(BUILD_DIR)/%.o:%.c 
	@echo .CC $<
	@mkdir -p $(subst ../,,$(@D))
	@$(CC) $(CFLAGS) -c $< $(INC) -o $(subst ../,,$@)

# as .S
$(BUILD_DIR)/%.o:%.S
	@echo .AS $<
	@mkdir -p $(subst ../,,$(@D))	 
	@$(AS) $(CFLAGS) -c $< $(INC) -o $(subst ../,,$@)

# elf
$(ELF):$(BUILD_DIR) $(OBJ)
	@echo link elf $@
	@$(CC) $(LINK_OBJ) $(LDFLAGS)  -o $@
	@$(SZ) $@ 

#cp .bin
$(BIN):$(ELF)
	@echo objcopy bin
	@$(CP) -O binary -S $< $@


$(DIS):$(ELF)
	@echo objdump dis
	@$(DP) -D -h -s $< > $@

clean:
	rm -rf $(BUILD_DIR)

d:
	make clean
	make all

c:$(BUILD_DIR) $(OBJC) $(OBJS)
	@echo .o

download:
	../../../../tools/wch_openocd/bin/openocd \
    -s ../../../../tools/wch_openocd/bin/ \
    -f wch-riscv.cfg \
    -c "program $(ELF) verify reset" \
    -c "reset run" \
    -c "exit"


test: $(OBJ) 
	@echo $(OBJ) 
	@echo $(SRC_C) 
	@echo $^

rebuild:
	make clean
	make all -j4
	make download